<template>
    <div class="last_audit">
        <div class="data_list">
            <el-table :data="tableData" :max-height="tableMaxHeight" style="width: 100%">
                <el-table-column align="left" prop="id" :label="$t('common.serialNumber')"></el-table-column><!--序号-->
                <el-table-column align="left" prop="name" :label="$t('common.accountName')"><!--账号名称-->
                    <template slot-scope="scope">
                        <a @click="searchAuditOrder(scope.row)" href="javascript:void(0);" :disabled="scope.row.isDelete == '1'">{{ scope.row.name }}</a>
                    </template>
                </el-table-column>
                <el-table-column align="left" prop="typeName" :label="$t('common.roleType')"><!--角色类型-->
                    <template slot-scope="scope">{{ $root.local == 'zhCN' ? scope.row.typeName : scope.row.typeIdName}}</template>
                </el-table-column>

                <el-table-column align="left" :label="$t('authority.accountStatus')" prop="roleName"><!--账号状态-->
                    <template slot-scope="scope">{{ scope.row.isDelete == '0' ? $t('common.enable') : $t('common.close')}}</template>
                </el-table-column>

                <el-table-column align="left" prop="remainOrderCount" :label="$t('authority.remainingOrder')"></el-table-column><!--剩余单量-->
                <el-table-column align="left" prop="maxNum" :label="$t('authority.everydayLimit')"><!--每日进件上限-->
                    <template slot-scope="scope">
                        <span @click="showSetNum(scope.row)"><i class="el-icon-edit"></i>{{ scope.row.maxNum }}</span>
                    </template>
                </el-table-column>
                <el-table-column align="left" prop="auditSwitch" :label="$t('common.isAssignOrder')"><!--是否进件-->
                    <template slot-scope="scope">
                        <el-switch @change="changeStatus(scope.row)" v-model="scope.row.auditSwitch" :disabled="scope.row.isDelete == '1'">{{ $t('common.isAssignOrder') }}</el-switch>
                    </template>
                </el-table-column>
            </el-table><!--end table-->
        </div>

        <pagination @pageChange="pageChange" :page="page">分页组件</pagination>

        <!--设置每日进件上限-->
        <el-dialog :visible.sync="showNumDialog" width="416px" :title="$t('authority.setEverydayUpperCase')">
            <el-form ref="form" :model="form" :rules="rules" label-position="top">
                <el-form-item prop="maxNum" :label="$t('authority.everydayLimit')"><!--每日进件上限-->
                    <el-input v-model="form.maxNum" type="text" :placeholder="$t('verify.plsInput')"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="showNumDialog = false" type="default">{{$t('common.cancel')}}</el-button>
                <el-button @click="setNum" type="primary">{{$t('common.submit')}}</el-button>
            </div>
        </el-dialog><!--设置每日进件上限end-->
    </div>
</template>

<script>
    import Pagination from '@/components/pagination';
    import mixin from '@/service/mixin';

    export default {
        name: 'lastAudit',
        components: {
            Pagination,
        },
        mixins: [mixin],
        props: {
            activeTab: {
                type: String,
                required: true
            },
            searchForm: {
                type: Object,
                required: true
            }
        },
        data() {
            return {
                thisRoleType: 2,
                form: {
                    auditorId: '',
                    maxNum: ''
                },
                showNumDialog: false,
                rules: {
                    maxNum: [
                        {required: true, message: this.$t('verify.plsInput'), trigger: 'blur'},
                        {pattern: /^\d+$/, message: this.$t('verify.plsInputInteger'), trigger: 'blur'}
                    ]
                }
            };
        },
        watch: {
            activeTab(val) {
                if (val === 'lastAudit') {
                    this.page.currentPage = 1;
                    this.getTableData();
                }
            },
            'searchForm.search'(val) {
                if (val && this.activeTab === 'lastAudit') {
                    this.page.currentPage = 1;
                    this.getTableData();
                }
            }
        },
        methods: {
            getTableData(page = this.page) {
                const params = {
                    type: this.thisRoleType,
                    name: this.searchForm.accountName,
                    isDelete: this.searchForm.isDelete,
                    leaderId: '',
                    page: {
                        pageNo: page.currentPage,
                        pageSize: this.page.pageSize
                    }
                };
                this.tableData = [];
                this.$api.authorityManage.getAllAuditorInfo(params).then((res) => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data.data) {
                        this.tableData = data.data;
                        this.page.recordsTotal = data.pageCount;
                        this.tableData.forEach((item) => {
                            item.auditSwitch = +item.auditSwitch === 1;
                        });
                        if (this.searchForm.search) {
                            this.$emit('search', this.searchForm);
                        }
                        this.tableMaxHeight = window.document.body.clientHeight - 265;
                    }
                });
            },
            changeStatus(row) {
                // console.log(row);
                const params = {
                    auditorId: row.auditorId,
                    auditSwitch: row.auditSwitch ? '1' : '0'
                };
                this.$api.authorityManage.updateAuditorInfo(params).then((res) => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error) {
                        this.$message.success(this.$t('common.successfulOperation'));
                        this.getTableData();
                    }
                });
            },
            setNum() {
                this.$refs.form.validate((val) => {
                    if (val) {
                        const params = {
                            auditorId: this.form.auditorId,
                            maxNum: this.form.maxNum
                        };
                        this.$api.authorityManage.updateAuditorInfo(params).then((res) => {
                            const {status, data, error} = res;
                            if ('1' === status && '00000000' === error) {
                                this.showNumDialog = false;
                                this.$message.success(this.$t('common.successfulOperation'));
                                this.getTableData();
                            }
                        });
                    }
                });
            },
            showSetNum(row) {
                this.form.auditorId = row.auditorId;
                this.form.maxNum = row.maxNum;
                this.showNumDialog = true;
            },
            searchAuditOrder(row) {
                this.$emit('getData', {
                    accountName: row.name,
                    roleType: '2',
                    auditorId: row.auditorId
                }, 'orderLastAudit');
            }
        },
        created() {
            if (this.activeTab === 'lastAudit') {
                this.getTableData();
            }
        }
    };
</script>

<style lang="scss" scoped>
    .data_list {
        background-color: #fff;
        padding: 10px 16px;
    }

    .el-icon-edit {
        margin-right: 10px;
    }
</style>
